package com.zk.mybatisplus.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zk.mybatisplus.test.dao.UserInfoMapper;
import com.zk.mybatisplus.test.model.UserInfoDO;
import com.zk.mybatisplus.test.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author kuN
 * @date 21:14
 */
@Service
@RequiredArgsConstructor
public class UserInfoServiceImpl implements UserInfoService {
    private final UserInfoMapper userInfoMapper;

    @Override
    public UserInfoDO getUserById(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    @Override
    public UserInfoDO getUserByParam(String name) {
        return userInfoMapper.selectOne(Wrappers.<UserInfoDO>lambdaQuery().eq(UserInfoDO::getName, name));
    }

    @Override
    public void saveUser(UserInfoDO userInfoDO) {
        userInfoMapper.insert(userInfoDO);
    }

    @Override
    public void updateUser(Long id, UserInfoDO userInfoDO) {
        userInfoDO.setId(id);
        userInfoMapper.updateById(userInfoDO);
    }

    @Override
    public IPage<UserInfoDO> getUserPage(String name, Integer page, Integer size) {
        IPage<UserInfoDO> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<UserInfoDO> wrapper = Wrappers.<UserInfoDO>lambdaQuery().eq(UserInfoDO::getName, name);
        return userInfoMapper.selectPage(pageParam, wrapper);
    }
}
